r_config.paths['registerAms'] = 'apps/ams/register';
r_config.paths['css-ams'] = 'assets/css/ams';
r_config.paths['mCommon'] = 'apps/ams/model/MCommon';
r_config.paths['mUser'] = 'apps/ams/model/MUser';
r_config.paths['mDepartment'] = 'apps/ams/model/MDepartment';
r_config.paths['mRole'] = 'apps/ams/model/MRole';
r_config.paths['mMenu'] = 'apps/ams/model/MMenu';
r_config.paths['mDict'] = 'apps/ams/model/MDict';
r_config.paths['mClassify'] = 'apps/ams/model/MClassify';
r_config.paths['mArchive'] = 'apps/ams/model/MArchive';
r_config.paths['mAttachment'] = 'apps/ams/model/MAttachment';
r_config.paths['mSearch'] = 'apps/ams/model/MSearch';
r_config.paths['mPrint'] = 'apps/ams/model/MPrint';

require.config(r_config);
require(['jquery', 'knockout', 'root', 'config','bootstrap'],
    function ($, ko, Root, Config) {
        Config.baseUrl = '/dhxy';
        Config.operationType = 'dropdown';
        require(['registerAms', 'util', 'router','mPassword', 'css!css-ams'], function (RegisterApp, Util, router,MPassword) {
            $(document).ready(
                function () {
                    RegisterApp.registerCmp();

                    Config.ajaxComplete = function(data) {
                        if(data.success==false&&data.logged == true){
                            logout();
                        }
                    };
                    //save the cmp for logout logic
                    ko.loginCmp = {
                        name: 'cmp:LoginMain',
                        params: {
                            url: Util.getRestUrl('/login'),
                            logo:'assets/images/login-logo.png',
                            //title: '档案网络管理系统',
                            onLoginSuccess: function (data) {
                                ko.root.user = data.data.user;
                                loadMenus();
                            }
                        }
                    };
                    ko.root = new Root({
                        fluid: false,
                        mainCmp: ko.loginCmp
                    });

                    ko.applyBindings(ko.root);

                    Util.ajaxGet({url:Util.getRestUrl('/login'),  callback:function (data) {
                        if (data.success) {
                            ko.root.user = data.data.user;
                            loadMenus();
                        }
                    }});

                    function loadMenus() {
                        Util.ajaxGet({url:Util.getRestUrl('/menus/user/menus'),
                            callback:function (data) {
                            if (data.success) {
                                ko.root.menus = data.data;
                                ko.root.topCmp({
                                    name: 'cmp:NavigationBar', params: {
                                        //title: '档案管理系统',
                                        brand: 'assets/images/brand.png',
                                        menuItems: ko.root.menus.findItemsByField({value:0,name: 'fatherId'}),
                                        shortcutItems: [{name: '', icon: 'user', subItems: ['修改密码','退出']}],
                                        shortcutClick: function (name) {
                                            switch (name) {
                                                case '修改密码':
                                                    var password = new MPassword({isKo:true});

                                                    password.setProxy({url:'/users/0/changePassword',type:'rest'})
                                                    ko.root.openModal({
                                                        operation: 'add', record: password, title: '修改密码'
                                                    });
                                                    break;
                                                case '退出':
                                                    Util.ajaxGet({url:Util.getRestUrl('/logout'), callback:function (data) {
                                                        if (data.success) {
                                                            logout();
                                                        }
                                                    }});
                                                    break;
                                                default:
                                            }
                                        }
                                    }
                                });

                                router.init();
                            }
                        }});
                    }

                    function logout() {
                        ko.root.topCmp(undefined);
                        ko.root.mainCmp.name(ko.loginCmp.name);
                        ko.root.mainCmp.params(ko.loginCmp.params);
                        router.destroy();
                    }
                });
        });
    });


